package com.ptszyxx.popose.module.main.dynamic;

import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;

import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentDynamicReleaseBinding;
import com.ptszyxx.popose.module.main.dynamic.vm.DynamicReleaseVM;
import com.ysg.base.BaseFragment;
import com.ysg.callback.EditAfterTextWatcher;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YEditTextUtil;
import com.ysg.utils.YPermissionUtil;
import com.ysg.widget.pic.SelectPicAdapter;
import com.ysg.widget.pic.SelectPicBean;
import com.ysg.widget.pic.SelectPicEnum;
import com.ysg.widget.pic.YPicUtils;

public class DynamicReleaseFragment extends BaseFragment<FragmentDynamicReleaseBinding, DynamicReleaseVM> {

    private SelectPicAdapter adapter;

    public DynamicReleaseFragment(){
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_dynamic_release;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public DynamicReleaseVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(DynamicReleaseVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestOosToken();
    }

    @Override
    public void initView() {
        super.initView();
        initContent();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onSubmitEvent.observe(this, o -> onSubmit());
        viewModel.uc.onSensitiveEvent.observe(this, o -> showDialogSensitive());
    }

    private void initContent() {
        binding.etContent.addTextChangedListener(new EditAfterTextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                String content = editable.toString().trim();
                YEditTextUtil.setContentMaxLength(content, 200, binding.tvNum, getResources().getString(R.string.dynamic_release_content_lenght));
            }
        });
    }

    @Override
    public void initAdapter() {
        adapter = new SelectPicAdapter(getContext(), SelectPicEnum.select);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        binding.recyclerView.setAdapter(adapter);

        //记得添加权限：
        adapter.setOnSelectListener(new SelectPicAdapter.OnSelectPicListener() {
            @Override
            public void onAddPicListener() {
                YPicUtils.getInstance().addAlbumAndVideo(getContext(), "申请拍照权限：用于发布个人动态图片", adapter, (result)->{
                    //这边要处理下：正常先上传图片后显示
                    for (LocalMedia item: result) {
                        SelectPicBean picBean = new SelectPicBean();
                        if (PictureMimeType.isHasVideo(item.getMimeType())){
                            picBean.setPic(item.getRealPath());
                            picBean.setMinPic(item.getRealPath());
                        }else{
                            picBean.setPic(item.getCompressPath());
                            picBean.setMinPic(item.getCompressPath());
                        }
                        adapter.getData().add(picBean);
                    }
                    adapter.notifyDataSetChanged();
                });
            }

            @Override
            public void onDeletePicListener(int position) {
            }
        });
    }

    private void onSubmit() {
        if (YCollectionUtil.isNotEmpty(adapter.getData())){
            viewModel.requestUploadPic(adapter.getData());
        }else{
            viewModel.requestDynamicRelease(null);
        }
    }

    private void showDialogSensitive(){
        YDialogUtil.getInstance().showConfirm(getContext(), getResources().getString(R.string.dialog_sensitive_content));
    }


}
